﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
// 郭嘉欣完成
namespace BirthdayPlan
{
    public partial class Input2 : Form
    {
        private int days = DataContainer.Data1;//距离生日的天数，用于初始化信息
        private string birthday = DataContainer.Birthday;//生日日期
        private string today =  DataContainer.Today;//今天日期
        private string daysUntilBirthday; //下次生日日期
        public Input2()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e) //计算计划日期
        {
            int daysBefore;
            if (int.TryParse(textBox1.Text, out daysBefore))
            {
                // 检查是否为负数
                if (daysBefore < 0 )
                {
                    MessageBox.Show("提前天数不能为负数，请输入正整数！", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    textBox1.Clear(); // 清空输入框
                    return; // 退出方法，避免后续计算
                }

                // 检查是否为超出阈值
                if (daysBefore > days)
                {
                    MessageBox.Show("提前日期超出阈值！请重新输入！", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    textBox1.Clear(); // 清空输入框
                    return; // 退出方法，避免后续计算
                }

                // 计算距离下次生日的日期
                DateTime nextBirthday = DateTime.Parse(daysUntilBirthday);
               

                    // 计算计划日期（提前 n 天）
                    DateTime planDate = nextBirthday.AddDays(-daysBefore);

                    DataContainer.Data2 = daysBefore;

                    // 调整计划日期为最近的周六
                    DateTime adjustedPlanDate = AdjustToNearestSaturday(planDate);

                    DataContainer.Planday= adjustedPlanDate.ToString("yyyy-MM-dd");

                    //result2 form2 = new result2();
                    //form2.Show();
                    //this.Hide();
                    // 显示给用户确认
                    DialogResult result = MessageBox.Show(
                        $"计划日期计算结果：{adjustedPlanDate:yyyy-MM-dd}\n是否确认并继续？",
                        "确认计划日期",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question);

                    // 如果用户点击 "Yes"，跳转到新的页面
                    if (result == DialogResult.Yes)
                    {
                        result1 form2 = new result1();
                        form2.Show();
                        this.Hide(); // 隐藏当前窗口
                    }
                }

                
            
            else
            {
                MessageBox.Show("请输入有效的天数！");
            }
        }

        private void Input2_Load(object sender, EventArgs e)
        {
            
            daysUntilBirthday = GetNextBirthday(birthday,today);
            DataContainer.Nextbirthday = daysUntilBirthday;

            // 显示下次生日日期和距离今天的天数
            label2.Text = "下次生日日期：" + daysUntilBirthday.ToString();
            label3.Text="距离生日还有：" + days.ToString() + "天";
        }

        public static string GetNextBirthday(string birthdayStr, string todayStr)
        {
            // 将字符串日期转换为 DateTime 类型
            DateTime birthday = DateTime.Parse(birthdayStr);
            DateTime today = DateTime.Parse(todayStr);

            int currentYear = today.Year;
            //DateTime nextBirthday = new DateTime(currentYear, birthday.Month, birthday.Day); //no 闰年29不合适
            // 如果生日是2月29日，且当前年份不是闰年，跳到下一个闰年
            if (birthday.Month == 2 && birthday.Day == 29)
            {
                // 如果当前年份不是闰年，找到下一个闰年
                while (!DateTime.IsLeapYear(currentYear))
                {
                    currentYear++;
                }
            }
            // 创建下一个生日日期
            DateTime nextBirthday = new DateTime(currentYear, birthday.Month, birthday.Day);


            // 检查是否已经过了今年的生日
            if (today > nextBirthday)
            {
                // 如果今天已经超过了今年的生日，下一次生日是明年
                nextBirthday = new DateTime(currentYear + 1, birthday.Month, birthday.Day);
            }



            // 将结果转换为字符串并返回
            return nextBirthday.ToString("yyyy-MM-dd");
        }

        private DateTime AdjustToNearestSaturday(DateTime date)
        {
            DayOfWeek dayOfWeek = date.DayOfWeek;

            // 如果日期是周一或周二，则调整为前一个周六
            if (dayOfWeek == DayOfWeek.Monday || dayOfWeek == DayOfWeek.Tuesday)
            {
                return date.AddDays(-(int)dayOfWeek - 1);  // 向前调整到最近的周六
            }
            // 如果日期是周三、周四、周五，则调整为后一个周六
            else if (dayOfWeek == DayOfWeek.Wednesday || dayOfWeek == DayOfWeek.Thursday || dayOfWeek == DayOfWeek.Friday)
            {
                return date.AddDays(6 - (int)dayOfWeek);  // 向后调整到最近的周六
            }

            // 如果是周六或周日，则保持原样
            return date;
        }
    }
}
